﻿using IntelligentGateway.Data;
using IntelligentGateway.Model;
using IntelligentGateway.Model.Panel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentGateway.Services
{
    public class SignalPointServices
    {
        public ResponseMessage InsertList(List<SignalPoint> List)
        {
            ResponseMessage response = new ResponseMessage();
            CommDA<SignalPoint> commDA = new CommDA<SignalPoint>();

            foreach (SignalPoint signalPoint in List)
            {
                if (signalPoint.Name.Contains("外呼上行登记后门"))
                {
                    signalPoint.TypeID = 4;
                    string strFloorID = signalPoint.Name.Replace("外呼上行登记后门", "");
                    if (string.IsNullOrEmpty(strFloorID))
                    {
                        response.errcode = 1;
                        response.errmsg = $"{signalPoint.Name}不包含楼层信息!";
                        return response;
                    }
                    signalPoint.FloorID = int.Parse(strFloorID);
                }
                else if (signalPoint.Name.Contains("外呼上行登记"))
                {
                    signalPoint.TypeID = 1;
                    string strFloorID = signalPoint.Name.Replace("外呼上行登记", "");
                    if (string.IsNullOrEmpty(strFloorID))
                    {
                        response.errcode = 1;
                        response.errmsg = $"{signalPoint.Name}不包含楼层信息!";
                        return response;
                    }
                    signalPoint.FloorID = int.Parse(strFloorID);
                }
                else if (signalPoint.Name.Contains("外呼下行登记后门"))
                {
                    signalPoint.TypeID = 5;
                    string strFloorID = signalPoint.Name.Replace("外呼下行登记后门", "");
                    if (string.IsNullOrEmpty(strFloorID))
                    {
                        response.errcode = 1;
                        response.errmsg = $"{signalPoint.Name}不包含楼层信息!";
                        return response;
                    }
                    signalPoint.FloorID = int.Parse(strFloorID);
                }
                else if (signalPoint.Name.Contains("外呼下行登记"))
                {
                    signalPoint.TypeID = 2;
                    string strFloorID = signalPoint.Name.Replace("外呼下行登记", "");
                    if (string.IsNullOrEmpty(strFloorID))
                    {
                        response.errcode = 1;
                        response.errmsg = $"{signalPoint.Name}不包含楼层信息!";
                        return response;
                    }
                    signalPoint.FloorID = int.Parse(strFloorID);
                }
                else if (signalPoint.Name.Contains("内呼登记后门"))
                {
                    string strFloorID = signalPoint.Name.Replace("内呼登记后门", "");
                    if (string.IsNullOrEmpty(strFloorID))
                    {
                        response.errcode = 1;
                        response.errmsg = $"{signalPoint.Name}不包含楼层信息!";
                        return response;
                    }
                    signalPoint.TypeID = 3;
                    signalPoint.FloorID = int.Parse(strFloorID);
                }
                else if (signalPoint.Name.Contains("内呼登记"))
                {
                    string strFloorID = signalPoint.Name.Replace("内呼登记", "");
                    if (string.IsNullOrEmpty(strFloorID))
                    {
                        response.errcode = 1;
                        response.errmsg = $"{signalPoint.Name}不包含楼层信息!";
                        return response;
                    }
                    signalPoint.TypeID = 0;
                    signalPoint.FloorID = int.Parse(strFloorID);
                }
                else
                {
                    signalPoint.TypeID = 0;
                }
            }
            string sqlInsert = "INSERT INTO signalpoint(NO,Name,RSL,FloorID,RecordValue,AuthorizeValue,TypeID, DeviceNO,DFloorID) VALUES (@NO,@Name,@RSL,@FloorID,@RecordValue, @AuthorizeValue,@TypeID, @DeviceNO,@DFloorID)";

            string result = commDA.InsertList(sqlInsert, List);
            if (!result.Contains("success"))
            {
                response.errcode = 1;
                response.errmsg = result;
            }
            CommServices.SignalPointList();
            return response;
        }

        public bool DeleteList(List<SignalPoint> List)
        {
            CommDA<SignalPoint> commDA = new CommDA<SignalPoint>();
            foreach (SignalPoint signalPoint in List)
            {
                string result = commDA.Delete(signalPoint);
                if (!result.Contains("success"))
                {
                    return false;
                }
            }

            return true;
        }

        public static void startCheckSignalPoint()
        {
            Task t = Task.Factory.StartNew(() =>
            {
                List<string> lstRemove = new List<string>();
                NetDev ndev = new NetDev();
                while (true)
                {
                    foreach (var item in GlobalVariables.SignalPointClearList)
                    {
                        if (DateTime.Now.Subtract(item.ClearTime).TotalSeconds > 30)
                        {
                            try
                            {
                                bool success = IGTCPServerMessageProcess.ProcessPointClear(item);
                                if (success)
                                {
                                    GlobalVariables.SignalPointClearList.Remove(item);
                                }
                            }
                            catch
                            {

                            }
                        }
                    }


                    Task.Delay(5000).Wait();
                }
            });
        }
    }
}
